/*
 * @Descripttion: 
 * @version: 
 * @Author: Qf
 * @Date: 2023-11-22 09:42:52
 * @LastEditTime: 2023-11-22 09:42:56
 * @LastEditors: Qf
 */


const path = require('path')
const _ = require('lodash')

// 获取platform mp-weixin
let platform = process.env.UNI_PLATFORM
platform = platform.replace(/mp-/g, '')


const routerFilePath = path.join(__dirname, `router/${platform}.js`)

const platformPageConfig = require(routerFilePath)


function customizer(objValue, srcValue) {
  if (_.isArray(objValue)) {
    return objValue.concat(srcValue);
  }
}

// pagesJson参数是pages.json值
module.exports = (pagesJson, loader) => {
  // 开发模式下才需要热重载
  if (['test', 'development'].includes(process.env.NODE_ENV)) {
    console.warn(`-----${process.env.NODE_ENV}----\n`)
    // 动态添加依赖，告诉webpack改文件需要热重载
    loader.addDependency(require.resolve(routerFilePath))
    // 手动清除缓存
    delete require.cache[require.resolve(routerFilePath)]
  }
  const pages = _.mergeWith({}, pagesJson, platformPageConfig, customizer)

  return pages
}